package com.ylbh.modules.sign.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.sign.dto.SignTurntableDTO;
import com.ylbh.common.pojo.sign.vo.ShoppingSigninPrizeVO;
import com.ylbh.common.pojo.sign.vo.ShoppingSigninTurntableInfoVO;
import com.ylbh.common.utils.OSSClientUtil;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.modules.sign.entity.ShoppingSigninPrize;
import com.ylbh.modules.sign.entity.ShoppingSigninTurntableInfo;
import com.ylbh.modules.sign.mapper.ShoppingSignPrizeMapper;
import com.ylbh.modules.sign.mapper.ShoppingSignTurntableMapper;
import com.ylbh.modules.sign.service.ShoppingSignPrizeService;
import com.ylbh.modules.sign.service.ShoppingSignTurntableService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wjc
 */
@Service
@Transactional
public class ShoppingSignTurntableServiceImpl implements ShoppingSignTurntableService {

    @Autowired
    private  ShoppingSignTurntableMapper shoppingSignTurntableMapper;
    @Autowired
    private ShoppingSignPrizeService shoppingSignPrizeService;
    @Autowired
    private ShoppingSignPrizeMapper shoppingSignPrizeMapper;
    @Override
    public PageUtils querySignTurntableList(SignTurntableDTO signTurntableDTO) {
        Page<Object>  page = new Page<>(signTurntableDTO.getPage(),signTurntableDTO.getLimit());
        List<ShoppingSigninTurntableInfoVO> turntableInfoVo = shoppingSignTurntableMapper.selectSignTurntableList(signTurntableDTO,page);
        // 查找该模板下的活动信息
        for (ShoppingSigninTurntableInfoVO vo :turntableInfoVo) {
            int count = shoppingSignPrizeService.getPrizeCountById(vo.getId());
            vo.setCount(count);
            List<ShoppingSigninPrizeVO> subPrize = shoppingSignTurntableMapper.selectSubSignPrizeListById(vo.getId());
            vo.setSubPrizeVo(subPrize);
        }
        Page<ShoppingSigninTurntableInfoVO> page1 = new Page<>();
        page1.setRecords(turntableInfoVo).setTotal(page.getTotal());
        return new PageUtils(page1);
    }


    @Override
    public String uploadTurntable(MultipartFile file , HttpServletRequest request) {
        OSSClientUtil ossClient = null;
        String path = request.getParameter("path");
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,path);//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/" + path + "/"+ imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    @Override
    public void addTurntable(SignTurntableDTO signTurntableDTO) {
        ShoppingSigninTurntableInfo info = new ShoppingSigninTurntableInfo();
        Date date = new Date();
        BeanUtils.copyProperties(signTurntableDTO,info);
        info.setCreateTime(date);
        info.setCreateUserId(ShiroUtils.getUserId().toString());
        shoppingSignTurntableMapper.insertShoppingSigninTurntableInfo(info);
        if (info.getId().toString() != null) {
            ShoppingSigninPrize prizeInfo = new ShoppingSigninPrize();
            prizeInfo.setTurnTableInfoId(info.getId().toString());
            prizeInfo.setPrizeType(1);
            prizeInfo.setPrizeRelation("88");
            prizeInfo.setPrizeName("可以修改为积分或其他");
            prizeInfo.setSort(1);
            prizeInfo.setPrizeTip("可以修改为积分或其他");
            prizeInfo.setPrizeProbability(new BigDecimal(100));
            prizeInfo.setPrizeNumber(-1);
            prizeInfo.setPrizeAllowance(-1);
            shoppingSignPrizeMapper.insertPrizeInfo(prizeInfo);
        }
    }

    @Override
    public void removeTurntableByIds(Integer[] ids) {
        try {
            shoppingSignTurntableMapper.deleteTurntableByIds(Arrays.asList(ids));
            shoppingSignPrizeMapper.deleteSignPrizeByTurnTableId(Arrays.asList(ids.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public ShoppingSigninTurntableInfo getTurntableById(Integer id) {
        return shoppingSignTurntableMapper.selectTurntableById(id);
    }

    @Override
    public void modifyTurntableById(SignTurntableDTO signTurntableDTO) {
        ShoppingSigninTurntableInfo info = new ShoppingSigninTurntableInfo();
        Date date = new Date();
        BeanUtils.copyProperties(signTurntableDTO,info);
        info.setUpdateTime(date);
        info.setUpdateUserId(ShiroUtils.getUserId().toString());
        shoppingSignTurntableMapper.updateShoppingSigninTurntableInfo(info);
    }

    @Override
    public Boolean modifyTurnTableEnableStateById(SignTurntableDTO signTurntableDTO) {
        if (signTurntableDTO.getEnableState() ==1) {
            int index = shoppingSignTurntableMapper.selectCountEnable();
            if (index >= 1) {
                return false;
            }else {
                shoppingSignTurntableMapper.updataTurnTableEnableStateById(signTurntableDTO);
                return true;
            }
        }else {
            shoppingSignTurntableMapper.updataTurnTableEnableStateById(signTurntableDTO);
            return true;
        }
    }
}
